<?php
namespace app\zhongcan\controller;
use app\model\NavModel;
use app\model\NewModel;
use app\validate\NavValidate;
use app\validate\NewValidate;
use cmf\controller\AdminBaseController;
use think\facade\Db;

class NavController extends AdminBaseController{
    public function index(){
        $dh=input('dh','');
        $where=[];
        if($dh){
            $where[]=['title','like',"%$dh%"];
        }
        $nav=Db::name('navigation')
            ->order('list_order','desc')
            ->where($where)
            ->paginate(1);
        $this->assign('nav',$nav);
        return $this->fetch();
    }
    public function add(){
        return $this->fetch();
    }
    public function addPost(){
        //接收
        $data=input('post.');
        $v=new NavValidate();
        $r=$v->scene('add')->check($data);
        if(!$r){
            $this->error($v->getError());
        }

        $model=NavModel::create($data);
        if($model){
            $this->success('添加成功','Nav/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id = input('id');
        $nav = Db::name('navigation')
            ->where('id', '=', $id)
            ->find();
        $this->assign('nav', $nav);
        return $this->fetch();
    }
    public function editPost(){
        //接收
        $data=input('post.');
        $v=new NavValidate();
        $r=$v->scene('edit')->check($data);
        if(!$r){
            $this->error($v->getError());
        }
        $model=NavModel::find($data['id']);

        $model->title=$data['title'];
        $model->url=$data['url'];
        $model->parent_id=$data['parent_id'];
        if($model->save()){
            $this->success('保存成功','Nav/index');
        }else{
            $this->error('保存失败');
        }
    }
    public function del(){
        $id=input('id/d');
        $model=NavModel::destroy($id);
        if($model){
            $this->success('删除成功','Nav/index');
        }else{
            $this->error('删除失败');
        }
    }
    public function px(){
        $model=new NavModel();
        parent::listOrders($model);
        $this->success('排序成功');
    }
}